//生成center_1的topo图
var ipList = new Array();
var nameList = new Array();

//得到表空间使用率数据
function gettableUserDataTop() {
	$.post('OracleInfo/getOracleTableUserInfo',{},function(data){
		var data = data.data;
		for ( var key in data) {
			if(data[key]>parseInt(oracletablespaceused)){
				var flag = true;
				for(var i = 0;i< nameList.length;i++){
					if(nameList[i] == 'ORACLE'){
						flag = false;
						break;
					}
				};
				if(flag){
					nameList.push('ORACLE');
				};
			}
		}
		getTopological();
	});
}

//得到各系统服务状态和端口状态
function getStatus() {
	$.post('connInfo/getNewAllAppStatus',{},function(data){
		var data = data.data;
		var statusmap = data[0];
		var portStutas = data[1];
		for ( var key in statusmap) {
			if(key=='数据库'){
				for ( var key2 in statusmap[key]) {
					if(statusmap[key][key2]== serviceth){
						var flag = true;
						for(var i = 0;i< nameList.length;i++){
							if(nameList[i] == key2){
								flag = false;
								break;
							}
						};
						if(flag){
							nameList.push(key2);
						};
					}
				}
			}else {
				if(statusmap[key] == 'off'){
					var flag = true;
					for(var m = 0;m< nameList.length;m++){
						if(nameList[m] == key){
							flag = false;
							break;
						}
					};
					if(flag){
						nameList.push(key);
					};
				}
			}
		};
		for ( var key in portStutas) {
			if(key=='数据库'){
				for ( var key1 in portStutas[key]) {
					for ( var key2 in portStutas[key][key1]) {
						if(portStutas[key][key1][key2]==portStatus){
							if(statusmap[key][key1]!=serviceth){
								var flag = true;
								for(var i = 0;i< nameList.length;i++){
									if(nameList[i] == key1){
										flag = false;
										break;
									}
								};
								if(flag){
									nameList.push(key1);
								};
							}
						}
					}
				}
			}else {
				for ( var key1 in portStutas[key]) {
					if(portStutas[key][key1]==portStatus){
						if(statusmap[key]!=serviceth){
							var flag = true;
							for(var i = 0;i< nameList.length;i++){
								if(nameList[i] == key){
									flag = false;
									break;
								}
							};
							if(flag){
								nameList.push(key);
							};
						}
					}
				}
				
			}
		};
		getmemInfoTop();
	});
}
//刷新各系统服务状态和端口状态
function freshStatus() {
	$.post('connInfo/getNewAllAppStatus',{},function(data){
		var data = data.data;
		var statusmap = data[0];
		var portStutas = data[1];
		for ( var key in statusmap) {
			if(key=='数据库'){
				for ( var key2 in statusmap[key]) {
					if(statusmap[key][key2]==serviceth){
						var flag = true;
						for(var i = 0;i< nameList.length;i++){
							if(nameList[i] == key2){
								flag = false;
								break;
							}
						};
						if(flag){
							nameList.push(key2);
						};
					}
				}
			}else {
				if(statusmap[key] == serviceth){
					var flag = true;
					for(var i = 0;i< nameList.length;i++){
						if(nameList[i] == key){
							flag = false;
							break;
						}
					};
					if(flag){
						nameList.push(key);
					};
				}
			}
		}
		for ( var key in portStutas) {
			if(key=='数据库'){
				for ( var key1 in portStutas[key]) {
					for ( var key2 in portStutas[key][key1]) {
						if(portStutas[key][key1][key2]==portStatus){
							if(statusmap[key][key1]!=serviceth){
								var flag = true;
								for(var i = 0;i< nameList.length;i++){
									if(nameList[i] == key1){
										flag = false;
										break;
									}
								};
								if(flag){
									nameList.push(key1);
								};
							}
						}
					}
				}
			}else {
				for ( var key1 in portStutas[key]) {
					if(portStutas[key][key1]==portStatus){
						if(statusmap[key]!=serviceth){
							var flag = true;
							for(var i = 0;i< nameList.length;i++){
								if(nameList[i] == key){
									flag = false;
									break;
								}
							};
							if(flag){
								nameList.push(key);
							};
						}
					}
				}
				
			}
		}
		getmemInfoTop();
	});
}



//得到应用连接数数据
function getAppConnNum() {
	$.post('connInfo/getAllConnNumList',{},function(data){
		var data= data.data;
		var conn = data[0];
		for ( var key in conn) {
			if(conn[key][conn[key].length-1] == data[1][key]){
				var flag = true;
				for(var i = 0;i< nameList.length;i++){
					if(nameList[i] == key){
						flag = false;
						break;
					};
				};
				if(flag){
					nameList.push(key);
				};
			}
		}
 //       console.info("getAllConnNumList:"+conn);
		getStatus();
	});
}
//刷新
function freshAppConnNum() {
	$.post('connInfo/getNewAllConnNum',{},function(data){
		var data = data.data;
		var conn = data[0];
		var maxConn = data[1];
		for ( var key in conn) {
			if(conn[key] == maxConn[key]){
				var flag = true;
				for(var i = 0;i< nameList.length;i++){
					if(nameList[i] == key){
						flag = false;
						break;
					}
				};
				if(flag){
					nameList.push(key);
				};
			}
		}
    //    console.info("getNewAllConnNum:"+conn);
		freshStatus();
	});
}
//得到 磁盘数据
function getDIskInfoStart() {
	$.post('diskInfo/getAllDiskinfo', {}, function(data) {
		var data = data.data;
		for ( var key1 in data) {
			var diskInfo = data[key1];
			for ( var key2 in diskInfo) {
				if (diskInfo[key2].diskUsedPer>parseInt(diskUsed)) {
					var flag = true;
					for(var i = 0;i< ipList.length;i++){
						if(ipList[i] == key1){
							flag = false;
							break;
						}
					};
					if(flag){
						ipList.push(key1);
					};
				}
			}
		}
     //   console.info("getAllDiskinfo:"+data);
		getAppConnNum();
	});
}

//刷新磁盘数据
function freshDIskInfo() {
	ipList.length = 0;
	nameList.length = 0;
	$.post('diskInfo/getAllDiskinfo', {}, function(data) {
		var data = data.data;
		for ( var key1 in data) {
			var diskInfo = data[key1];
			for ( var key2 in diskInfo) {
				if (diskInfo[key2].diskUsedPer>parseInt(diskUsed)) {
					var flag = true;
					for(var i = 0;i< ipList.length;i++){
						if(ipList[i] == key1){
							flag = false;
							break;
						}
					};
					if(flag){
						ipList.push(key1);
					};
				}
			}
		}
		freshAppConnNum();
	});
}

// 得到 CPU数据
function getCpuInfoTop() {
	$.post('cpuInfo/getAllCpuinfo', {}, function(data) {
		var data = data.data;
		for ( var key in data) {
			if (data[key].used > parseInt(cpuUsed)) {
				var flag = true;
				for(var i = 0;i< ipList.length;i++){
					if(ipList[i] == key){
						flag = false;
						break;
					}
				};
				if(flag){
					ipList.push(key);
				};
			};
			if(data[key].iowaite > parseInt(cpuIowait)){
				var flag = true;
				for(var i = 0;i< ipList.length;i++){
					if(ipList[i] == key){
						flag = false;
						break;
					}
				};
				if(flag){
					ipList.push(key);
				};
			};
		}
		gettableUserDataTop();
	});
}
//得到内存数据
function getmemInfoTop() {
	$.post('MemInfo/getAllMeminfo', {}, function(data) {
		var data = data.data;
		for ( var key in data) {
			if (data[key].memUsed > parseInt(memUsed)) {
				var flag = true;
				for(var i = 0;i< ipList.length;i++){
					if(ipList[i] == key){
						flag = false;
						break;
					};
				};
				if(flag){
					ipList.push(key);
				};
			};
			if(data[key].swapUsed >parseInt(swapUsed)){
				var flag = true;
				for(var i = 0;i< ipList.length;i++){
					if(ipList[i] == key){
						flag = false;
						break;
					};
				};
				if(flag){
					ipList.push(key);
				};
			};
		};
		getCpuInfoTop();
	});
}


//得到网络拓扑图
function getTopological() {
    var nodes = null;
    var edges = null;
    var network = null;
 //   var DIR = '../img/refresh-cl/';
    var EDGE_LENGTH_MAIN = 150;
    var EDGE_LENGTH_SUB = 130;

    // Called when the Visualization API is loaded.
      // Create a data table with nodes.
    $.post('ServerInfo/getAllserverInfoByIp',{},function(data){	
    	var data = data.data;
    	nodes = [];
    	var nodeId=2;
    	var esb;
    	var mysql;
    	var oracle=[];
    	var appId=[];
    	var nameSys = [];
    	// Create a data table with links.
    	edges = [];
    	//nodes.push({id: 1, label: '', image:'image/host.png', shape: 'image'});
    	for ( var key in data) {
    		var appkey = 1;
    		var appIp = '';
    		var app='';
    		for ( var key1 in data[key]) {
				if(data[key][key1].name=='ESB'){
					var flag = true;
					var flag2 = true;
					for(var i = 0;i < nameList.length;i++){
						if(data[key][key1].name == nameList[i]){
							nodes.push({id: nodeId, label: key+'\n中医馆数据交汇', image:'image/server2.png', shape: 'image'});
							flag2 = false;
							break;
						}
					};
					if(flag2){
						for(var i = 0;i < ipList.length;i++){
							if(key == ipList[i]){
								nodes.push({id: nodeId, label: key+'\n中医馆数据交汇', image:'image/server2.png', shape: 'image'});
								flag = false;
								break;
							}
						};
					};
					if(flag && flag2){
						nodes.push({id: nodeId, label: key+'\n中医馆数据交汇', image:'image/server.png', shape: 'image'});
					};
					//edges.push({from: 1, to: nodeId, length: 90});
					esb=nodeId;					
				}else if (data[key][key1].name=='MYSQL') {
					var flag = true;
					var flag2 = true;
					for(var i = 0;i < nameList.length;i++){
						if(data[key][key1].name == nameList[i]){
							nodes.push({id: nodeId, label: key+'\nMYSQL', image:'image/server2.png', shape: 'image'});
							flag2 = false;
							break;
						}
					};
					if(flag2){
						for(var i = 0;i < ipList.length;i++){
							if(key == ipList[i]){
								nodes.push({id: nodeId, label: key+'\nMYSQL', image:'image/server2.png', shape: 'image'});
								flag = false;
								break;
							}
						};
					};
					if(flag && flag2){
						nodes.push({id: nodeId, label: key+'\nMYSQL', image:'image/server.png', shape: 'image'});
					};
					oracle.push(nodeId);
				}else if (data[key][key1].name=='ORACLE') {
					var ora = new Array();
					ora = key.split(",");
					var flag = true;
					var flag2 = true;
					for(var i = 0;i < nameList.length;i++){
						if(data[key][key1].name == nameList[i]){
							nodes.push({id: nodeId, label: key+'\nORACLE', image:'image/server2.png', shape: 'image'});
							flag2 = false;
							break;
						}
					};
					if(flag2){
						for(var i = 0;i < ipList.length;i++){
							for(var j = 0;j < ora.length;j++){
								if(ora[j] == ipList[i]){
									nodes.push({id: nodeId, label: key+'\nORACLE', image:'image/server2.png', shape: 'image'});
									flag = false;
									break;
								}
							};
							if(!flag){
								break;
							};
						};
					};
					if(flag && flag2){
						nodes.push({id: nodeId, label: key+'\nORACLE', image:'image/server.png', shape: 'image'});
					};
					oracle.push(nodeId);
				}else {
					if(appkey==1){
						appIp=key;
						appkey+=1;
					}
					app+=data[key][key1].name+'\n';
					nameSys.push(data[key][key1].name);
				}
			}
    		if (key==appIp) {
    			var flag = true;
    			var flag2 = true;
				for(var i = 0;i < nameList.length;i++){
					for (var j = 0;j < nameSys.length;j++) {
						if(nameSys[j] == nameList[i]){
							nodes.push({id: nodeId, label: key+app, image:'image/server2.png', shape: 'image'});
							flag2 = false;
							nameSys.length = 0;
							break;
						}
					};
					if(!flag2){
						break;
					}
				};
				if(flag2){
					for(var i = 0;i < ipList.length;i++){
						if(key == ipList[i]){
			    			nodes.push({id: nodeId, label: key+app, image:'image/server2.png', shape: 'image'});
							flag = false;
							break;
						}
					};
				};
				if(flag && flag2){
	    			nodes.push({id: nodeId, label: key+app, image:'image/server.png', shape: 'image'});
				};
    			appId.push(nodeId);
			}
    		nodeId+=1;
    	}
    	for (var int = 0; int < appId.length; int++) {
    		edges.push({from: esb, to: appId[int], length: EDGE_LENGTH_SUB});
		}
    	
    	for (var int2 = 0; int2 < oracle.length; int2++) {
			edges.push({from: esb, to: oracle[int2], length: EDGE_LENGTH_SUB});
		}
    	
    	// create a network
    	var container = document.getElementById('topological');
    	var data = {
    			nodes: nodes,
    			edges: edges
    	};
    	var options = {
    			nodes:{
    				font: {//字体配置
    					color: '#EEE',//颜色
    					size: 10, // 大小，单位px
    					face: 'arial',//字体
    					background: 'none',//背景
    					align: 'left',//位置left right center
    				},
    			}
    	};
    	network = new vis.Network(container, data, options);
    	ipList.length = 0;
    	nameList.length = 0;
    });
}